import MSData
import MSLogger

def get_FromSGN_LastRecId(dbCursor):
    query = "SELECT MAX(ID) FROM SGN"
    try:
        dbCursor.execute(query)
        buff = dbCursor.fetchall()
        recId = int(buff[0][0])
        return recId + 1
    except (Exception), err:
        return 1

def get_FromObj_ObjectsConfig(dbCursor):
    query = "SELECT "

def store_MSEvent_ToObj(event, dbCursor):
    if not event.hasMobile:
        return
    recordSpeed = event.GPSSpeed
    recordLat = event.GPSLatitude
    recordLon = event.GPSLongitude
    recordTime = event.eventGenerateTime.strftime(MSData.TIME_FORMAT_PCN)
    recordObj = event.objectId
    query = "UPDATE obj SET LAT='%s', LON='%s', SPD='%s', D_ONLINE='%s' WHERE ID='%s'" % (recordLat, recordLon, recordSpeed, recordTime, recordObj)
    try:
        dbCursor.execute(query)
        MSLogger.logDebug('query: %s' % (query))
    except (Exception), err:
        MSLogger.logError('failed to execute query: %s exception: %s' % (query, str(err)))

def store_MSEvent_ToSGN(event, dbCursor, recId):
    if not event.hasStatic:
        return
    if event.contactIdType == None:
        return recId
    recordId        = str(recId)
    recordIdMes     = str(event.contactIdType)
    recordIdObj     = str(event.objectId)
    recordSDate     = str(event.eventGenerateTime.strftime(MSData.TIME_FORMAT_PCN))
    recordXUser     = 'magicsys'
    recordPhone     = '434343'
    recordIDPr      = '2'
    recordIDPPL     = '-1'
    recordIDZon     = '-1'
    recordZona      = '0'
    recordRazdel    = '1'
    recordSTR1      = '09'
    recordSTR2      = '5011 180994R65500000'

    query = "INSERT INTO SGN (ID, ID_MES, ID_OBJ, SDATE, XUSER, PHONE, ID_PR, ID_PPL, ID_ZON, ZONA, RAZDEL, STR1, STR2) VALUES" + \
                             "('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')" % \
                             (recordId, recordIdMes, recordIdObj, recordSDate, recordXUser, recordPhone, recordIDPr, recordIDPPL, \
                              recordIDZon, recordZona, recordRazdel, recordSTR1, recordSTR2)

    try:
        dbCursor.execute(query)
        MSLogger.logDebug('query: %s' % (query))
        recId += 1
    except (Exception), err:
        MSLogger.logDebug('query: %s' % (query))
        MSLogger.logError('failed to execute query: %s exception: %s' % (query, str(err)))
    return recId

def store_MSEvent_ToRecords(event, dbCursor, recId):
    if not event.hasMobile:
        return recId
    recordSpeed = event.GPSSpeed
    recordLat = event.GPSLatitude
    recordLon = event.GPSLongitude
    recordTime = event.eventGenerateTime.strftime(MSData.TIME_FORMAT_PCN)
    recordObj = event.objectId

    query = "INSERT INTO records (ID_OBJ, LAT, LON, SPD, REC_DATE) VALUES ('%s', '%s', '%s', '%s', '%s')" % (recordObj, recordLat, recordLon, recordSpeed, recordTime)
    try:
        dbCursor.execute(query)
        MSLogger.logDebug('query: %s' % (str(query)))
        recId += 1
    except (Exception), err:
        MSLogger.logError('failed to execute query: %s exception: %s' % (query, str(err)))
    return recId

